package com.swinginwind.flexutils.components
{
	/**
	 * @author jimmy
	 * @E-mail: swinginwind@gmail.com
	 * @version 1.0.0
	 * 创建时间：2013-1-16 下午1:57:44
	 * 
	 */
	import mx.controls.ComboBox;
	import mx.events.FlexEvent;
	import mx.events.ListEvent;
	
	public class CustomComboBox extends ComboBox
	{
		public function CustomComboBox()
		{
			//TODO: implement function
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE, registerEvent);
			
		}
		
		private function registerEvent(event:FlexEvent):void
		{
			this.removeEventListener(FlexEvent.CREATION_COMPLETE, registerEvent);
			if(dropdown)
				dropdown.addEventListener(ListEvent.CHANGE, updateLabel);
		}
		
		private var _label:String; 
		
		public var treeSelectedItem:Object; 
		public function updateLabel(event:*):void{
			treeSelectedItem = event.currentTarget.selectedItem; 
			if(treeSelectedItem && treeSelectedItem.hasOwnProperty(this.labelField))
				_label = treeSelectedItem[this.labelField];     
			else
				_label = treeSelectedItem.toString();
			
		} 
		
		override protected function updateDisplayList(unscaledWidth:Number,
													  unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);  
			
			if(dropdown && _label != null){   
				text = _label; 
			} 
		}
		
		override public function set dataProvider(data:Object):void
		{
			super.dataProvider = data;
			if(dropdown)
				dropdown.dataProvider = data;
		}
	}
}